/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseStaffDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseStaffDao : BaseDao, IBaseDao
    {
        public BaseStaffDao()
        {
            base.CurrentTableName = BaseStaffTable.TableName;
        }

        public BaseStaffDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseStaffDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseStaffDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseStaffEntity baseStaffEntity)
        {
            return this.AddEntity(baseStaffEntity);
        }

        public string Add(string departmentID, string userName, string code, string fullName, bool isVirtual, bool isDimission, bool enabled, string description)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseStaffTable.TableName);
            builder.BeginInsert(BaseStaffTable.TableName);
            builder.SetValue(BaseStaffTable.FieldID, sequence);
            builder.SetValue(BaseStaffTable.FieldCode, code);
            builder.SetValue(BaseStaffTable.FieldUsername, userName);
            builder.SetValue(BaseStaffTable.FieldRealname, fullName);
            builder.SetValue(BaseStaffTable.FieldDepartmentID, departmentID);
            builder.SetValue(BaseStaffTable.FieldSortCode, sequence);
            builder.SetValue(BaseStaffTable.FieldEnabled, enabled ? 1 : 0);
            builder.SetValue(BaseStaffTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseStaffTable.FieldCreateDate);
            return ((builder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public string AddEntity(BaseStaffEntity baseStaffEntity)
        {
            if (string.IsNullOrEmpty(baseStaffEntity.ID) || string.IsNullOrEmpty(baseStaffEntity.SortCode))
            {
                string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseStaffTable.TableName);
                if (string.IsNullOrEmpty(baseStaffEntity.ID))
                {
                    baseStaffEntity.ID = sequence;
                }
                if (string.IsNullOrEmpty(baseStaffEntity.SortCode))
                {
                    baseStaffEntity.SortCode = sequence;
                }
            }
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseStaffTable.TableName);
            sqlBuilder.SetValue(BaseStaffTable.FieldID, baseStaffEntity.ID);
            this.SetEntity(sqlBuilder, baseStaffEntity);
            sqlBuilder.SetValue(BaseStaffTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseStaffTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return baseStaffEntity.ID;
        }

        public string AddStaff(string companyID, string departmentID, string dutyID, string titleID, string userName, string realName, string code, string roleID, string sex, string userID, bool enabled, string description, out string statusCode)
        {
            string str = string.Empty;
            new BaseStaffDao(base.DbHelper, base.UserInfo);
            if (this.Exists(BaseStaffTable.FieldUsername, userName))
            {
                statusCode = StatusCode.ErrorUsernameExist.ToString();
                return str;
            }
            if ((code.Length > 0) && this.Exists(BaseStaffTable.FieldCode, code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            str = this.AddStaffEntity(companyID, departmentID, dutyID, titleID, userName, realName, code, roleID, sex, userID, enabled, description);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        private string AddStaffEntity(string companyID, string departmentID, string dutyID, string titleID, string userName, string realname, string code, string role, string sex, string userID, bool enabled, string description)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseStaffTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseStaffTable.TableName);
            builder.SetValue(BaseStaffTable.FieldID, sequence);
            builder.SetValue(BaseStaffTable.FieldCompanyID, companyID);
            builder.SetValue(BaseStaffTable.FieldDepartmentID, departmentID);
            builder.SetValue(BaseStaffTable.FieldDutyID, dutyID);
            builder.SetValue(BaseStaffTable.FieldTitleID, titleID);
            builder.SetValue(BaseStaffTable.FieldUsername, userName);
            builder.SetValue(BaseStaffTable.FieldRealname, realname);
            builder.SetValue(BaseStaffTable.FieldCode, code);
            builder.SetValue(BaseStaffTable.FieldSex, sex);
            builder.SetValue(BaseStaffTable.FieldUserID, userID);
            builder.SetValue(BaseStaffTable.FieldEnabled, enabled ? 1 : 0);
            builder.SetValue(BaseStaffTable.FieldDescription, description);
            builder.SetValue(BaseStaffTable.FieldSortCode, sequence);
            builder.SetValue(BaseStaffTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseStaffTable.FieldCreateDate);
            return ((builder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public override int BatchDelete(string[] ids)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                num += this.Delete(ids[i]);
            }
            return num;
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseStaffTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.DeleteEntity(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseStaffTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    BaseStaffEntity baseStaffEntity = new BaseStaffEntity(row);
                    num += this.UpdateEntity(baseStaffEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    BaseStaffEntity entity2 = new BaseStaffEntity(row);
                    num += (this.AddEntity(entity2).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            base.ReturnStatusCode = StatusCode.OK.ToString();
            return num;
        }

        public BaseUserInfo ConvertToUserInfo(BaseStaffEntity staffEntity, BaseUserInfo userInfo)
        {
            userInfo.StaffID = staffEntity.ID;
            userInfo.Code = staffEntity.Code;
            userInfo.Username = staffEntity.Username;
            userInfo.Realname = staffEntity.Realname;
            userInfo.CompanyID = staffEntity.CompanyID;
            userInfo.CompanyCode = staffEntity.CompanyCode;
            userInfo.CompanyFullName = staffEntity.CompanyFullName;
            userInfo.DepartmentID = staffEntity.DepartmentID;
            userInfo.DepartmentCode = staffEntity.DepartmentCode;
            userInfo.DepartmentFullName = staffEntity.DepartmentFullName;
            userInfo.WorkgroupID = staffEntity.WorkgroupID;
            userInfo.WorkgroupCode = staffEntity.WorkgroupCode;
            userInfo.WorkgroupFullName = staffEntity.WorkgroupFullName;
            return userInfo;
        }

        public override int Delete(string id)
        {
            int num = 0;
            BaseStaffEntity entity = this.GetEntity(id);
            num += DbLogic.Delete(base.DbHelper, BaseUserRoleTable.TableName, BaseUserRoleTable.FieldUserID, entity.UserID);
            BaseUserDao dao = new BaseUserDao(base.DbHelper, base.UserInfo);
            num += dao.DeleteEntity(entity.UserID);
            return (num + DbLogic.Delete(base.DbHelper, BaseStaffTable.TableName, BaseStaffTable.FieldID, id));
        }

        public int DeleteUser(string staffID)
        {
            string userID = this.GetEntity(staffID).UserID;
            if (!string.IsNullOrEmpty(userID))
            {
                new BaseUserDao(base.DbHelper, base.UserInfo).Delete(userID);
            }
            return this.SetProperty(staffID, BaseStaffTable.FieldUserID, null);
        }

        public DataTable GetAddressDT()
        {
            return this.GetAddressDT(string.Empty, string.Empty);
        }

        public DataTable GetAddressDT(string organizeID)
        {
            return this.GetAddressDT(organizeID, string.Empty);
        }

        public DataTable GetAddressDT(string organizeID, string search)
        {
            if (organizeID == null)
            {
                organizeID = string.Empty;
            }
            search = BaseBusinessLogic.GetSearchString(search);
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".* ," + BaseOrganizeTable.TableName + "A." + BaseOrganizeTable.FieldCode + " AS CompanyCode ," + BaseOrganizeTable.TableName + "A." + BaseOrganizeTable.FieldFullName + " AS CompanyFullName ," + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldCode + " AS DepartmentCode ," + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldFullName + " AS DepartmentFullName ,ECP_ItemDetails_Duty." + BaseItemDetailsTable.FieldItemName + " AS DutyFullName ,OT.RoleFullName  FROM " + BaseStaffTable.TableName + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " " + BaseOrganizeTable.TableName + "A ON " + BaseOrganizeTable.TableName + "A." + BaseOrganizeTable.FieldID + " = " + BaseStaffTable.FieldCompanyID + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " " + BaseOrganizeTable.TableName + "B ON " + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldID + " = " + BaseStaffTable.FieldDepartmentID + "      LEFT OUTER JOIN ECP_ItemDetails_Duty ON ECP_ItemDetails_Duty." + BaseItemDetailsTable.FieldID + " = " + BaseStaffTable.FieldDutyID + "      LEFT OUTER JOIN (SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + "," + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " AS RoleFullName  FROM " + BaseUserTable.TableName + "," + BaseRoleTable.TableName + " WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + " = " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldID + ") OT                   ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " = OT.ID ";
            if (string.IsNullOrEmpty(organizeID))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE ((" + BaseOrganizeTable.TableName + "A." + BaseOrganizeTable.FieldIsInnerOrganize + " = 1) OR (" + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldIsInnerOrganize + " =1)) ";
            }
            else
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + " WHERE (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldCompanyID + " = '" + organizeID + "' OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " = '" + organizeID + "') ";
            }
            if (!string.IsNullOrEmpty(search))
            {
                string str4 = sqlQuery;
                string str5 = str4 + " AND (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldUsername + " LIKE '" + search + "'";
                string str6 = str5 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldRealname + " LIKE '" + search + "'";
                string str7 = str6 + " OR " + BaseOrganizeTable.TableName + "A." + BaseOrganizeTable.FieldFullName + " LIKE '" + search + "'";
                string str8 = str7 + " OR " + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldFullName + " LIKE '" + search + "'";
                string str9 = str8 + " OR ECP_ItemDetails_Duty." + BaseItemDetailsTable.FieldItemName + " LIKE '" + search + "'";
                string str10 = str9 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldOfficePhone + " LIKE '" + search + "'";
                string str11 = str10 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldMobile + " LIKE '" + search + "'";
                string str12 = str11 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldShortNumber + " LIKE '" + search + "'";
                string str13 = str12 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldOICQ + " LIKE '" + search + "'";
                string str14 = str13 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldEmail + " LIKE '" + search + "'";
                sqlQuery = (str14 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDescription + " LIKE '" + search + "'") + " OR OT.RoleFullName LIKE '" + search + "')";
            }
            string str15 = sqlQuery;
            sqlQuery = str15 + " ORDER BY " + BaseOrganizeTable.TableName + "B." + BaseOrganizeTable.FieldSortCode + " ," + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public string GetCategoryCount(IDbHelper dbHelper, string categoryID, string organizeCode, string categoryField)
        {
            string sqlQuery = string.Empty;
            string str2 = string.Empty;
            string[] targetFileds = new string[3];
            object[] targetValues = new object[3];
            sqlQuery = " SELECT COUNT(ID) AS STAFFCOUNT FROM " + BaseStaffTable.TableName + " WHERE (" + categoryField + " = ?) AND (ENABLED = 1) AND (ISDIMISSION <> 1) AND (ISSTAFF = 1) AND (DepartmentID IN (SELECT ID FROM " + BaseOrganizeTable.TableName + " WHERE (LEFT(CODE, LEN(?)) = ?))) ";
            targetFileds[0] = categoryField;
            targetFileds[1] = BaseOrganizeTable.FieldCode;
            targetFileds[2] = organizeCode;
            targetValues[0] = categoryID;
            targetValues[1] = organizeCode;
            targetValues[2] = organizeCode;
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            if (obj2 != null)
            {
                str2 = obj2.ToString();
            }
            return str2;
        }

        public override DataTable GetDT()
        {
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".*  , " + BaseUserTable.TableName + ".UserOnLine ,(SELECT " + BaseOrganizeTable.FieldCode + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".CompanyID) AS CompanyCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".CompanyID) AS CompanyFullname  ,(SELECT " + BaseOrganizeTable.FieldCode + " From " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".DepartmentID) AS DepartmentCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".DepartmentID) AS DepartmentFullName  ,(SELECT " + BaseOrganizeTable.FieldCode + " From " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".WorkgroupID) AS WorkgroupCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".WorkgroupID) AS WorkgroupFullName  ,(SELECT " + BaseItemDetailsTable.FieldItemName + " FROM ECP_ItemDetails_Duty WHERE ID = " + BaseStaffTable.TableName + ".DutyID) AS DutyFullname  ,(SELECT " + BaseItemDetailsTable.FieldItemName + " FROM ECP_ItemDetails_Title WHERE ID = " + BaseStaffTable.TableName + ".TitleID) AS TitleFullname  ,(SELECT " + BaseRoleTable.FieldRealname + " FROM " + BaseRoleTable.TableName + " WHERE ID = Role) AS RoleFullName  FROM (" + BaseStaffTable.TableName + " LEFT OUTER JOIN " + BaseUserTable.TableName + " ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " = " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + ")   LEFT OUTER JOIN " + BaseOrganizeTable.TableName + "  ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " = " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldID + " ORDER BY " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldSortCode + " , " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetDT(string name, string value)
        {
            string sqlQuery = " SELECT A.*  ,(SELECT Code FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.TableName + ".ID = A.CompanyID) AS CompanyCode ,(SELECT FullName FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.TableName + ".ID = A.CompanyID) AS CompanyFullName  ,(SELECT Code FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.TableName + ".ID = A.DepartmentID) AS DepartmentCode ,(SELECT FullName FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.TableName + ".ID = A.DepartmentID) AS DepartmentFullName  ,(SELECT " + BaseOrganizeTable.FieldCode + " From " + BaseOrganizeTable.TableName + " WHERE ID = A.WorkgroupID) AS WorkgroupCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = A.WorkgroupID) AS WorkgroupFullName  ,(SELECT ItemName FROM ECP_ItemDetails_Duty WHERE ECP_ItemDetails_Duty.ID = A.DutyID) AS DutyFullName  ,(SELECT ItemName FROM ECP_ItemDetails_Title WHERE ECP_ItemDetails_Title.ID = A.TitleID) AS TitleFullName  FROM " + BaseStaffTable.TableName + " A  WHERE " + name + " = " + base.DbHelper.GetParameter(name) + " ORDER BY A.SortCode";
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(name, value));
            return dataTable;
        }

        public DataTable GetDTByDepartment(string departmentID)
        {
            string sqlQuery = " SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldUserOnLine + ", " + BaseStaffTable.TableName + ".*  ,(SELECT " + BaseOrganizeTable.FieldCode + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".CompanyID) AS CompanyCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".CompanyID) AS CompanyFullname  ,(SELECT " + BaseOrganizeTable.FieldCode + " From " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".DepartmentID) AS DepartmentCode ,(SELECT " + BaseOrganizeTable.FieldFullName + " FROM " + BaseOrganizeTable.TableName + " WHERE ID = " + BaseStaffTable.TableName + ".DepartmentID) AS DepartmentFullName  ,(SELECT " + BaseItemDetailsTable.FieldItemName + " FROM ECP_ItemDetails_Duty WHERE ID = " + BaseStaffTable.TableName + ".DutyID) AS DutyFullname  ,(SELECT " + BaseItemDetailsTable.FieldItemName + " FROM ECP_ItemDetails_Title WHERE ID = " + BaseStaffTable.TableName + ".TitleID) AS TitleFullname  ,(SELECT " + BaseRoleTable.FieldRealname + " FROM " + BaseRoleTable.TableName + " WHERE ID = Role) AS RoleFullName  FROM " + BaseStaffTable.TableName + " LEFT OUTER JOIN " + BaseUserTable.TableName + "       ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " = " + BaseUserTable.TableName + "." + BaseUserTable.FieldID;
            if (!string.IsNullOrEmpty(departmentID))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " = '" + departmentID + "' ";
            }
            string str3 = sqlQuery;
            sqlQuery = str3 + " ORDER BY " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetDTByOrganize(string organizeID)
        {
            string[] organizeIDs = new string[] { organizeID };
            return this.GetDTByOrganizes(organizeIDs);
        }

        public DataTable GetDTByOrganizes(string[] organizeIDs)
        {
            string str = BaseBusinessLogic.ObjectsToList(organizeIDs);
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".* ," + BaseUserTable.TableName + ".UserOnLine  FROM " + BaseStaffTable.TableName + " LEFT OUTER JOIN " + BaseUserTable.TableName + "       ON " + BaseStaffTable.TableName + ".ID = " + BaseUserTable.TableName + ".ID  WHERE " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldWorkgroupID + " IN ( " + str + ")        OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " IN (" + str + ")        OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldCompanyID + " IN (" + str + ")  ORDER BY " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public BaseStaffEntity GetEntity(string id)
        {
            BaseStaffEntity entity = new BaseStaffEntity(this.Get(id));
            BaseOrganizeDao dao = new BaseOrganizeDao(base.DbHelper);
            entity.CompanyCode = dao.GetCode(entity.CompanyID);
            entity.CompanyFullName = dao.GetFullName(entity.CompanyID);
            entity.DepartmentCode = dao.GetCode(entity.DepartmentID);
            entity.DepartmentFullName = dao.GetFullName(entity.DepartmentID);
            return entity;
        }

        public string GetStaffCount(string organizeCode)
        {
            string sqlQuery = string.Empty;
            string str2 = string.Empty;
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            sqlQuery = " SELECT COUNT(ID) AS STAFFCOUNT FROM " + BaseStaffTable.TableName + " WHERE (ENABLED = 1) AND (ISDIMISSION <> 1) AND (ISSTAFF = 1) AND (DepartmentID IN (SELECT ID FROM " + BaseOrganizeTable.TableName + " WHERE (LEFT(CODE, LEN(?)) = ?))) ";
            targetFileds[0] = BaseStaffTable.FieldCompanyID;
            targetValues[0] = organizeCode;
            object obj2 = base.DbHelper.ExecuteScalar(sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            if (obj2 != null)
            {
                str2 = obj2.ToString();
            }
            return str2;
        }

        public int ResetSortCode()
        {
            int num = 0;
            DataTable dT = this.GetDT();
            string id = string.Empty;
            string targetValue = string.Empty;
            foreach (DataRow row in dT.Rows)
            {
                id = row[BaseStaffTable.FieldID].ToString();
                targetValue = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseStaffTable.TableName);
                num += this.SetProperty(id, BaseStaffTable.FieldSortCode, targetValue);
            }
            return num;
        }

        public DataTable Search(string organizeID, string search, bool deleteMark)
        {
            search = BaseBusinessLogic.GetSearchString(search);
            string sqlQuery = string.Concat(new object[] {
                " SELECT ", BaseStaffTable.TableName, ".* ,", BaseOrganizeTable.TableName, ".", BaseOrganizeTable.FieldFullName, " AS DepartmentFullName ,ECP_ItemDetails_Duty.", BaseItemDetailsTable.FieldItemName, " AS DutyFullName ,OT.RoleFullName  FROM ", BaseStaffTable.TableName, "      LEFT OUTER JOIN ", BaseOrganizeTable.TableName, " ON ", BaseOrganizeTable.TableName, ".", BaseOrganizeTable.FieldID,
                " = ", BaseStaffTable.FieldDepartmentID, "      LEFT OUTER JOIN ECP_ItemDetails_Duty ON  ECP_ItemDetails_Duty.", BaseItemDetailsTable.FieldID, " = ", BaseStaffTable.FieldDutyID, "      LEFT OUTER JOIN (SELECT ", BaseUserTable.TableName, ".", BaseUserTable.FieldID, ",", BaseRoleTable.TableName, ".", BaseRoleTable.FieldRealname, " AS RoleFullName  FROM ", BaseUserTable.TableName,
                ",", BaseRoleTable.TableName, " WHERE ", BaseUserTable.TableName, ".", BaseUserTable.FieldRole, " = ", BaseRoleTable.TableName, ".", BaseRoleTable.FieldID, ") OT                   ON ", BaseStaffTable.TableName, ".", BaseStaffTable.FieldID, " = OT.ID  WHERE (", BaseStaffTable.FieldDeleteMark,
                " = ", deleteMark ? 1 : 0, ")"
             });
            if (!string.IsNullOrEmpty(organizeID))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " AND (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " = '" + organizeID + "')";
            }
            if (!string.IsNullOrEmpty(search))
            {
                string str3 = sqlQuery;
                string str4 = str3 + " AND (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldUsername + " LIKE '" + search + "'";
                string str5 = str4 + " OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldRealname + " LIKE '" + search + "'";
                string str6 = str5 + " OR " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " LIKE '" + search + "'";
                sqlQuery = (str6 + " OR ECP_ItemDetails_Duty." + BaseItemDetailsTable.FieldItemName + " LIKE '" + search + "'") + " OR OT.RoleFullName LIKE '" + search + "')";
            }
            string str7 = sqlQuery;
            sqlQuery = str7 + " ORDER BY " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable Search(string userName, string enabled, string role)
        {
            return this.SearchByOrganizeIDs(null, userName, enabled, role);
        }

        public DataTable SearchByOrganizeIDs(string[] organizeIDs, string userName, string enabled, string role)
        {
            string sqlQuery = " SELECT " + BaseUserTable.TableName + ".* ," + BaseUserTable.TableName + "." + BaseUserTable.FieldUserOnLine + " ,(SELECT " + BaseRoleTable.FieldRealname + " FROM " + BaseRoleTable.TableName + " WHERE " + BaseRoleTable.FieldID + " = " + BaseUserTable.FieldRole + ") AS RoleFullName  FROM " + BaseUserTable.TableName + " LEFT OUTER JOIN " + BaseStaffTable.TableName + " ON " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + " = " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldID + " WHERE 1=1 ";
            if (organizeIDs != null)
            {
                string str2 = BaseBusinessLogic.ObjectsToList(organizeIDs);
                string str3 = sqlQuery;
                string str4 = str3 + " AND (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldWorkgroupID + " IN (" + str2 + ") ";
                string str5 = str4 + "     OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " IN (" + str2 + ") ";
                sqlQuery = str5 + "     OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldCompanyID + " IN (" + str2 + ")) ";
            }
            if (!string.IsNullOrEmpty(userName))
            {
                if (userName.IndexOf('%') < 0)
                {
                    userName = "%" + userName + "%";
                }
                userName = base.DbHelper.SqlSafe(userName);
            }
            if (!string.IsNullOrEmpty(userName))
            {
                string str6 = sqlQuery;
                sqlQuery = str6 + " AND UPPER(" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldUsername + ") LIKE UPPER('" + userName + "') ";
            }
            if (!string.IsNullOrEmpty(enabled))
            {
                string str7 = sqlQuery;
                sqlQuery = str7 + " AND " + BaseUserTable.TableName + "." + BaseUserTable.FieldEnabled + " = '" + enabled + "'";
            }
            if (!string.IsNullOrEmpty(role))
            {
                string str8 = sqlQuery;
                sqlQuery = str8 + " AND " + BaseUserTable.FieldRole + " = '" + role + "'";
            }
            string str9 = sqlQuery;
            sqlQuery = str9 + " ORDER BY " + BaseUserTable.TableName + "." + BaseUserTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public override int SetDeleteMark(string id)
        {
            string property = this.GetProperty(id, BaseStaffTable.FieldUserID);
            if (!string.IsNullOrEmpty(property))
            {
                new BaseUserDao(base.DbHelper, base.UserInfo).SetDeleteMark(property);
            }
            return this.SetProperty(BaseBusinessLogic.FieldID, id, BaseBusinessLogic.FieldDeleteMark, "1");
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseStaffEntity baseStaffEntity)
        {
            sqlBuilder.SetValue(BaseStaffTable.FieldUsername, baseStaffEntity.Username);
            sqlBuilder.SetValue(BaseStaffTable.FieldRealname, baseStaffEntity.Realname);
            sqlBuilder.SetValue(BaseStaffTable.FieldCode, baseStaffEntity.Code);
            sqlBuilder.SetValue(BaseStaffTable.FieldSex, baseStaffEntity.Sex);
            sqlBuilder.SetValue(BaseStaffTable.FieldCompanyID, baseStaffEntity.CompanyID);
            sqlBuilder.SetValue(BaseStaffTable.FieldDepartmentID, baseStaffEntity.DepartmentID);
            sqlBuilder.SetValue(BaseStaffTable.FieldDutyID, baseStaffEntity.DutyID);
            sqlBuilder.SetValue(BaseStaffTable.FieldIDCard, baseStaffEntity.IDCard);
            sqlBuilder.SetValue(BaseStaffTable.FieldBankCode, baseStaffEntity.BankCode);
            sqlBuilder.SetValue(BaseStaffTable.FieldEmail, baseStaffEntity.Email);
            sqlBuilder.SetValue(BaseStaffTable.FieldMobile, baseStaffEntity.Mobile);
            sqlBuilder.SetValue(BaseStaffTable.FieldShortNumber, baseStaffEntity.ShortNumber);
            sqlBuilder.SetValue(BaseStaffTable.FieldTelephone, baseStaffEntity.Telephone);
            sqlBuilder.SetValue(BaseStaffTable.FieldOICQ, baseStaffEntity.OICQ);
            sqlBuilder.SetValue(BaseStaffTable.FieldOfficePhone, baseStaffEntity.OfficePhone);
            sqlBuilder.SetValue(BaseStaffTable.FieldOfficeZipCode, baseStaffEntity.OfficeZipCode);
            sqlBuilder.SetValue(BaseStaffTable.FieldOfficeAddress, baseStaffEntity.OfficeAddress);
            sqlBuilder.SetValue(BaseStaffTable.FieldOfficeFax, baseStaffEntity.OfficeFax);
            sqlBuilder.SetValue(BaseStaffTable.FieldHomePhone, baseStaffEntity.HomePhone);
            sqlBuilder.SetValue(BaseStaffTable.FieldAge, baseStaffEntity.Age);
            sqlBuilder.SetValue(BaseStaffTable.FieldWed, baseStaffEntity.Wed);
            sqlBuilder.SetValue(BaseStaffTable.FieldBirthday, baseStaffEntity.Birthday);
            sqlBuilder.SetValue(BaseStaffTable.FieldEducation, baseStaffEntity.Education);
            sqlBuilder.SetValue(BaseStaffTable.FieldSchool, baseStaffEntity.School);
            sqlBuilder.SetValue(BaseStaffTable.FieldDegree, baseStaffEntity.Degree);
            sqlBuilder.SetValue(BaseStaffTable.FieldTitleID, baseStaffEntity.TitleID);
            sqlBuilder.SetValue(BaseStaffTable.FieldTitleDate, baseStaffEntity.TitleDate);
            sqlBuilder.SetValue(BaseStaffTable.FieldTitleLevel, baseStaffEntity.TitleLevel);
            sqlBuilder.SetValue(BaseStaffTable.FieldWorkingDate, baseStaffEntity.WorkingDate);
            sqlBuilder.SetValue(BaseStaffTable.FieldJoinInDate, baseStaffEntity.JoinInDate);
            sqlBuilder.SetValue(BaseStaffTable.FieldHomeZipCode, baseStaffEntity.HomeZipCode);
            sqlBuilder.SetValue(BaseStaffTable.FieldHomeAddress, baseStaffEntity.HomeAddress);
            sqlBuilder.SetValue(BaseStaffTable.FieldHomeFax, baseStaffEntity.HomeFax);
            sqlBuilder.SetValue(BaseStaffTable.FieldNativePlace, baseStaffEntity.NativePlace);
            sqlBuilder.SetValue(BaseStaffTable.FieldParty, baseStaffEntity.Party);
            sqlBuilder.SetValue(BaseStaffTable.FieldNation, baseStaffEntity.Nation);
            sqlBuilder.SetValue(BaseStaffTable.FieldNationality, baseStaffEntity.Nationality);
            sqlBuilder.SetValue(BaseStaffTable.FieldMajor, baseStaffEntity.Major);
            sqlBuilder.SetValue(BaseStaffTable.FieldWorkingProperty, baseStaffEntity.WorkingProperty);
            sqlBuilder.SetValue(BaseStaffTable.FieldCompetency, baseStaffEntity.Competency);
            sqlBuilder.SetValue(BaseStaffTable.FieldIsDimission, baseStaffEntity.IsDimission);
            sqlBuilder.SetValue(BaseStaffTable.FieldDimissionDate, baseStaffEntity.DimissionDate);
            sqlBuilder.SetValue(BaseStaffTable.FieldDimissionCause, baseStaffEntity.DimissionCause);
            sqlBuilder.SetValue(BaseStaffTable.FieldDimissionWhither, baseStaffEntity.DimissionWhither);
            sqlBuilder.SetValue(BaseStaffTable.FieldEnabled, baseStaffEntity.Enabled);
            sqlBuilder.SetValue(BaseStaffTable.FieldSortCode, baseStaffEntity.SortCode);
            sqlBuilder.SetValue(BaseStaffTable.FieldDescription, baseStaffEntity.Description);
        }

        public int Update(BaseStaffEntity baseStaffEntity)
        {
            return this.UpdateEntity(baseStaffEntity);
        }

        public int UpdateAddress(BaseStaffEntity staffEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseStaffTable.TableName, staffEntity.ID, staffEntity.ModifyUserID, staffEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            num = this.UpdateEntity(staffEntity);
            if (num == 1)
            {
                this.UpdateUser(staffEntity.ID);
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseStaffEntity baseStaffEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseStaffTable.TableName);
            this.SetEntity(sqlBuilder, baseStaffEntity);
            sqlBuilder.SetValue(BaseStaffTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseStaffTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseStaffTable.FieldID, baseStaffEntity.ID);
            return sqlBuilder.EndUpdate();
        }

        public int UpdateStaff(string id, string companyID, string departmentID, string workgroupID, string dutyID, string titleID, string code, string realName, string userID, string userName, string sex, bool enabled, string description, out string statusCode)
        {
            int num = 0;
            if ((code.Length > 0) && this.Exists(BaseStaffTable.FieldCode, code, id))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            if (!string.IsNullOrEmpty(userName) && this.Exists(BaseStaffTable.FieldUsername, userName, id))
            {
                statusCode = StatusCode.ErrorUsernameExist.ToString();
                return num;
            }
            num = this.UpdateStaffEntity(id, companyID, departmentID, workgroupID, dutyID, titleID, code, realName, userID, userName, sex, enabled, description);
            this.UpdateUser(id);
            if (num > 0)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        private int UpdateStaffEntity(string id, string companyID, string departmentID, string workgroupID, string dutyID, string titleID, string code, string realName, string userID, string userName, string sex, bool enabled, string description)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseStaffTable.TableName);
            builder.SetValue(BaseStaffTable.FieldUsername, userName);
            builder.SetValue(BaseStaffTable.FieldRealname, realName);
            builder.SetValue(BaseStaffTable.FieldCode, code);
            builder.SetValue(BaseStaffTable.FieldCompanyID, companyID);
            builder.SetValue(BaseStaffTable.FieldDepartmentID, departmentID);
            builder.SetValue(BaseStaffTable.FieldWorkgroupID, workgroupID);
            builder.SetValue(BaseStaffTable.FieldDutyID, dutyID);
            builder.SetValue(BaseStaffTable.FieldSex, sex);
            builder.SetValue(BaseStaffTable.FieldUserID, userID);
            builder.SetValue(BaseStaffTable.FieldDescription, description);
            builder.SetValue(BaseStaffTable.FieldEnabled, enabled ? 1 : 0);
            builder.SetValue(BaseStaffTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseStaffTable.FieldModifyDate);
            builder.SetWhere(BaseStaffTable.FieldID, id);
            return builder.EndUpdate();
        }

        public int UpdateUser(string staffID)
        {
            BaseStaffEntity entity = new BaseStaffEntity(this.GetDT(BaseStaffTable.FieldID, staffID));
            if (!string.IsNullOrEmpty(entity.UserID))
            {
                BaseUserEntity entity2 = new BaseUserDao(base.DbHelper, base.UserInfo).GetEntity(entity.UserID);
                entity2.Company = entity.CompanyFullName;
                entity2.Department = entity.DepartmentFullName;
                entity2.Workgroup = entity.WorkgroupFullName;
                entity2.Username = entity.Username;
                entity2.Realname = entity.Realname;
                entity2.Code = entity.Code;
                entity2.Email = entity.Email;
                entity2.Enabled = entity.Enabled;
                entity2.Duty = entity.DutyFullName;
                entity2.Title = entity.TitleFullName;
                entity2.Sex = entity.Sex;
                entity2.Birthday = entity.Birthday;
                entity2.HomeAddress = entity.HomeAddress;
                entity2.Mobile = entity.Mobile;
            }
            return 0;
        }
    }
}